var ns, toast;
var app = new Vue({
    el: '#app',
    data: {
        showCalender: false,
        choosedatetime: '',
        show: true,
        list:[
            {timer:'2022-09-01',scoreType:1,scoreSec:'正常',type:1},
            {timer:'2022-09-02',scoreType:2,scoreSec:'迟到',type:1},
            {timer:'2022-09-03',scoreType:3,scoreSec:'早退',type:2},
            {timer:'2022-09-04',scoreType:4,scoreSec:'缺卡',type:2},
            {timer:'2022-09-05',scoreType:1,scoreSec:'正常',type:2},
            {timer:'2022-09-06',scoreType:5,scoreSec:'请假',type:1},
            {timer:'2022-09-07',scoreType:1,scoreSec:'正常',type:2},
            {timer:'2022-09-08',scoreType:1,scoreSec:'正常',type:1},
        ],
        dateType: 'd',//日期类型：d-日，w-周，m-月
        selectTime: '',//选择的日期类型对应的时间
        weekList: [],
        monthList: [],
        nowYear: 0,//初始年
        nowWeek: 0,//初始周
        yearActiveMonth: 0,
        nowMonth: 0,//初始月

        showNowWeekInMonth: [],

        showyear: false,
        minDate: new Date(2000, 0, 1),
        maxDate: new Date(2050, 12, 1),
        currentDate: new Date(),

        GPSLocation:'',
        nowTime:'',
        minDateRili:'',
        minDateRili: new Date(2022,8,1),
        maxDateRili: new Date(),
        day0fnowMonth:'',



    },
    created() {
        this.yearActiveMonth = moment().format('MM');

    },
    methods: {
        //选择日、周、月
        choosetime: function (dateType) {
            var that = this;
            that.dateType = dateType;
            if (that.dateType == 'd') {
                that.nowYear = moment().format('YYYY');
                that.selectTime = moment().format('MM.DD');
            }
            if (that.dateType == 'w') {
                that.nowYear = moment().format('YYYY');
                var startTime = moment().startOf('week');
                var endTime = moment().endOf('week');
                that.selectTime = moment(startTime).add(1, 'days').format('MM.DD')
                    + '-' + moment(endTime).add(1, 'days').format('MM.DD');
                that.weekList = that.get_week_year(moment().format('YYYY'), 1);

                let monthWeek = that.weekList[that.yearActiveMonth - 1]
                that.showNowWeekInMonth = monthWeek.filter(res => res.length > 0)

                // const m = moment().format('MM')
                const day = moment().format('YYYY-MM-DD')
                that.nowWeek = that.get_days_in_monthweek(that.yearActiveMonth, day)
                console.log(JSON.stringify(that.nowWeek));
            }
            if (that.dateType == 'm') {
                that.nowYear = moment().format('YYYY');
                that.nowMonth = moment().format('M');
                that.selectTime = moment().format('M') + '月';
                that.monthList = that.get_week_year(moment().format('YYYY'), 2);
                setTimeout(() => {
                    that.start_month();
                }, 100);
            }
        },
        chooseyue: function (index) {
            var that = this;
            console.log(JSON.stringify(index));
            that.nowMonth = index + 1;
            console.log(JSON.stringify(that.nowMonth));
            // that.start_month();
            that.selectTime = that.nowMonth + '月';
        },
        chooseWeek: function (index, item) {
            var that = this;
            that.nowWeek = index + 1;
            let start = moment(item[0]).format('MM.DD');
            let end = moment(item[6]).format('MM.DD');
            that.selectTime = start + '-' + end;
        },
        // 下一个月
        nextMonth: function () {
            var that = this;
            that.yearActiveMonth = Number(that.yearActiveMonth) + 1;
            console.log(JSON.stringify(that.yearActiveMonth));
            if (that.yearActiveMonth > 12) {
                that.showyear = true;
            } else {
                console.log(JSON.stringify(that.yearActiveMonth));
                let monthWeek = that.weekList[that.yearActiveMonth - 1]
                that.showNowWeekInMonth = monthWeek.filter(res => res.length > 0)
                console.log(JSON.stringify(that.showNowWeekInMonth));
                let start = moment(that.showNowWeekInMonth[0][0]).format('MM.DD');
                let end = moment(that.showNowWeekInMonth[0][6]).format('MM.DD');
                that.selectTime = start + '-' + end;
                that.nowWeek = 1;
            }
        },
        //上一个月
        beforeMonth: function () {
            var that = this;
            that.yearActiveMonth = Number(that.yearActiveMonth) - 1;
            console.log(JSON.stringify(that.yearActiveMonth));
            // return;
            if (that.yearActiveMonth < 1) {
                that.showyear = true;
            } else {
                console.log(JSON.stringify(that.yearActiveMonth));
                let monthWeek = that.weekList[that.yearActiveMonth - 1]
                that.showNowWeekInMonth = monthWeek.filter(res => res.length > 0)
                console.log(JSON.stringify(that.showNowWeekInMonth));
                let start = moment(that.showNowWeekInMonth[0][0]).format('MM.DD');
                let end = moment(that.showNowWeekInMonth[0][6]).format('MM.DD');
                that.selectTime = start + '-' + end;
                that.nowWeek = 1;
            }
        },


        get_week_year: function (y, type) {
            var that = this;
            // const ymd=y+'-'+moment().format('MM-DD');
            let yArr = [];
            let ym = '';
            for (let i = 1; i <= 12; i++) {
                if (i < 10) {
                    i = '0' + i
                }
                ym = moment(y + '-' + i).format('YYYY-MM');
                var m = [];
                if (type == 1) {
                    m = that.get_week_month(ym);
                } else {
                    m = that.get_month(ym);
                }
                yArr.push(m);
            }
            return yArr;
        },
        get_week_month: function (ym) {
            var that = this;
            const ymd = ym + '-01'
            var firstday = moment(ymd).startOf('month');
            var endday = moment(ymd).endOf('month');

            var firstweek = moment(firstday).format('d');
            var endweek = moment(endday).format('d');
            var daycount = 0;
            if (firstweek > 0) {
                daycount = firstweek - 1;
            } else {
                daycount = 6;
            }
            var lastday = moment(endday).subtract(endweek, 'days');
            let daysArr = [];
            for (let i = daycount; i > 0; i--) {
                let d = moment(firstday).subtract(i, 'days').format('YYYY-MM-DD');
                daysArr.push(d);
            }
            for (let i = 1; i <= moment(lastday).format('D'); i++) {
                if (i < 10) {
                    i = '0' + i
                }
                let d = moment(ym + '-' + i).format('YYYY-MM-DD');
                daysArr.push(d);
            }

            //补充下一个月日期满一个周
            // for (let i = 1; i <= 7 - endweek; i++) {
            //     let d = moment(endday).add(i, 'days').format('YYYY-MM-DD');
            //     daysArr.push(d);
            // }
            var arrLen = 7;
            var newDataArr = [];
            for (let i = 0; i <= daysArr.length; i += arrLen) {
                newDataArr.push(daysArr.slice(i, i + arrLen));
            }
            // newDataArr=newDataArr.filter(res=>res.length>0);
            return newDataArr;
            console.log(JSON.stringify(newDataArr));
        },
        get_month: function (ym) {
            var that = this;
            // var ym='2022-08';
            const ymd = ym + '-01'
            var lastday = moment(ymd).endOf('month');
            var daysArr = [];
            for (let i = 1; i <= moment(lastday).format('D'); i++) {
                if (i < 10) {
                    i = '0' + i
                }
                let d = moment(ym + '-' + i).format('YYYY-MM-DD');
                daysArr.push(d);
            }
            return daysArr;

        },
        get_days_in_monthweek: function (month, day) {
            var that = this;
            let monthWeek = that.weekList[month - 1]
            monthWeek = monthWeek.filter(res => res.length > 0)
            let inMonthweek = 0
            for (let i = 0; i < monthWeek.length; i++) {
                const mw = monthWeek[i] || []
                for (let ii = 0; ii < mw.length; ii++) {
                    const d = mw[ii];
                    if (moment(d).startOf('day').unix() === moment(day).startOf('day').unix()) inMonthweek = i + 1
                }
            }
            return inMonthweek
        },

        getCount(day) {
            var that = this;
            var myDate = new Date(day.date);
            if (!myDate) {
                return 0;
            }
            var y = myDate.getFullYear();
            var m = myDate.getMonth() + 1;
            var d = myDate.getDate();
            var totalCount = 0;

            that.list.forEach(function (item) {
                var lsDate = new Date(item.timer);
                var lsy = lsDate.getFullYear();
                var lsm = lsDate.getMonth() + 1;
                var lsd = lsDate.getDate();
                if (y == lsy && m == lsm && d == lsd) {
                    totalCount = item.scoreType;
                    return false;
                }
            })
            return totalCount;
        },
        onConfirm(day) {
            var that = this;
            var y = day.getFullYear();
            var m = day.getMonth() + 1;
            var d = day.getDate();
            var nowdate = y + '.' + (m >= 10 ? m : '0' + m) + '.' + (d >= 10 ? d : '0' + d);
            that.showCalender = false;
            that.choosedatetime = nowdate;
            // that.getdata();
        },
        //选中项定位
        start_month: function () {
            var that = this;
            /**
             * 1)先让选中的元素滚到可视区域的最左边 scrollLeft
             * 2)接着向右移动容器一半的距离 containWidth / 2
             * 3)最后向左移动item一半的距离 offsetWidth / 2
             */
            let lastSpot = that.$refs.scrollBox.scrollLeft;
            const nextSpace = 7 //每次移动距离
            let scrollItemTimer = setInterval(() => {
                this.$nextTick(() => {
                    let offsetWidth = that.$refs.scrollItem[that.nowMonth].offsetWidth //item
                    let scrollLeft = that.$refs.scrollItem[that.nowMonth].offsetLeft //选中的元素滚到可视区域的最左边
                    const containWidth = that.$refs.scrollBox.offsetWidth //容器的宽度
                    let resultSpot = scrollLeft + offsetWidth / 2 - containWidth / 2 //最终要停留的点
                    console.log(JSON.stringify(resultSpot));
                    if (Math.abs(lastSpot - resultSpot) < nextSpace) {
                        clearInterval(scrollItemTimer)
                    }
                    if (resultSpot >= lastSpot) {
                        lastSpot = lastSpot + nextSpace
                    } else {
                        lastSpot = lastSpot - nextSpace
                    }
                    that.$refs.scrollBox.scrollTo(lastSpot, 0)
                })
            }, 0.1)
        },
        open_year: function () {
            var that = this;
            if(that.dateType!='d'){
                that.showyear = true;
            }
        },
        closeYear:function(){
            var that = this;
                that.showyear = false;
        },
        formatter(type, val) {
            if (type === 'year') {
                return `${val}年`;
            } else if (type === 'month') {
                return `${val}月`;
            }
            return val;
        },
        doSureConfirm: function (value) {
            var that = this;
            if (that.dateType == 'w') {
                var time = moment(value).format('YYYY-MM');
                var list = that.get_week_month(time);
                that.showNowWeekInMonth = list.filter(res => res.length > 0)
                let start = moment(that.showNowWeekInMonth[0][0]).format('MM.DD');
                let end = moment(that.showNowWeekInMonth[0][6]).format('MM.DD');
                that.selectTime = start + '-' + end;
                that.nowWeek = 1;
                that.yearActiveMonth = moment(value).format('MM');
            }
            if (that.dateType == 'm') {
                var time = moment(value).format('YYYY');
                var list = that.get_week_year(time);
                that.monthList = list.filter(res => res.length > 0)
                that.selectTime = 2+'月';
                that.nowMonth = 2;
                setTimeout(() => {
                    that.start_month();
                }, 100);
                // that.yearActiveMonth = moment(value).format('MM');
            }

            that.nowYear = moment(value).format('YYYY');

            that.showyear = false;

        },

        StartBmap: function() {
            var that = this;
            ns.confirmPer('location', function() {
                that.StartLocation();
            })
        },
        StartLocation: function() {
            var that = this;
            var num = 0; //次数
            that.GPSLocation = '';
            bmLocation.stopLocation();
            bmLocation.singleLocation({
                reGeocode: true,
                netWorkState: true
            }, function(ret) {
                if (ret.status) {
                    that.GPSLocation = ret.reGeo.province + ret.reGeo.city +
                        ret.reGeo.district + ret.reGeo.street + ret.reGeo.streetNumber;
                }
            });
        },
        configManager: function() {
            bmLocation.configManager({
                accuracy: 'hight_accuracy',
                filter: 1,
                activityType: 'automotiveNavigation',
                coordinateType: 'BMK09LL',
                locationTimeout: 10,
                reGeocodeTimeout: 10
            });
        },
        getTime:function(){
            var that=this;
            that.nowTime=moment().format('YYYY-MM-DD HH:mm:ss');
        },




    }
});

apiready = function () {
    ns = window.Foresight.Util;
    toast = new auiToast();
    app.nowTime=moment().format('YYYY-MM-DD HH:mm:ss');
    app.day0fnowMonth=moment().format('YYYY-MM-DD');
    setInterval(app.getTime, 1000);
    bmLocation = api.require('bmLocation');
    app.nowYear = moment().format('YYYY');
    app.selectTime = moment().format('MM.DD');
    // app.get_week();
    // console.log(JSON.stringify(app.minDateRili));
}
